<template>
  <el-dialog title="审核合同"
             :visible.sync="dialogForm"
             width="1200px">
    <el-form :inline="true"
             ref="form"
             :model="form"
             class="add-form"
             label-width="110px">
      <h3>基础信息</h3>
      <hr />
      <br />
      <el-row>
        <el-col :span="8"
                class="el-height">合同号：{{form.code}}</el-col>
        <el-col :span="8"
                class="el-height">账户名称：{{form.cargoOwnerAccountName}}</el-col>
        <el-col :span="8"
                class="el-height">客户名称：{{form.partyAName}}</el-col>
        <el-col :span="8"
                class="el-height">有效期起：{{form.startDate}}</el-col>
        <el-col :span="8"
                class="el-height">有效期止：{{form.endDate}}</el-col>
        <el-col :span="8"
                class="el-height">合同名称：{{form.name}}</el-col>
        <el-col :span="8"
                class="el-height">签订时间:{{form.signDate}}</el-col>
        <el-col :span="8"
                class="el-height">免保管费:{{form.ifFreeSafeguard}}</el-col>
        <el-col :span="8"
                class="el-height">入库结算:{{form.inSettlementModeDesc}}</el-col>
        <el-col :span="8"
                class="el-height">出库结算:{{form.outSettlementModeDesc}}</el-col>
        <el-col :span="8"
                class="el-height">所属单位:{{form.orgName}}</el-col>
        <el-col :span="8"
                class="el-height">所属仓库:{{form.warehouseName}}</el-col>
        <el-col :span="8"
                class="el-height">备注:{{form.remark}}</el-col>
      </el-row>
      <h3>合同附件</h3>
      <hr />
      <br />
      <el-row>
        <el-col :span="12">
          <el-form-item label="附件"
                        class="customFormWidth">
            <attachments v-model="form.attachments"></attachments>
            <download v-model="form.attachments"></download>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="3">
          <h3>计费规则设置 </h3>
        </el-col>
      </el-row>
      <hr />
      <br />
      <el-row class="box"
              v-for="(item,index) in form.contractRules"
              :key="index">

        <el-row class="title">
          <el-col :span="22">
            <h3>{{item.feeRuleName}}</h3>
          </el-col>
        </el-row>
        <el-row class="block">
          <el-col>
            有效期:{{item.startDate}}~{{item.endDate}}
          </el-col>
        </el-row>
        <el-table :data="item.rates"
                  border>
          <el-table-column label="序号"
                           width="50">
            <template slot-scope="scope">
              {{ scope.$index+1 }}
            </template>
          </el-table-column>
          <el-table-column :key="item2.id"
                           :label="item2.name"
                           v-for="(item2, index2) in item.tableList">
            <template slot-scope="scope">
              <el-col :span="12">
                <el-select v-model="item.rates[scope.$index].factor[index2].operator"
                           placeholder="请选择"
                           disabled
                           clearable>
                  <el-option v-for="[key, val] in _dictRuleOperator"
                             :key="key"
                             :label="val"
                             :value="key"></el-option>
                </el-select>
              </el-col>
              <el-col :span="12">
                <el-select v-model="item.rates[scope.$index].factor[index2].typeValue"
                           placeholder="请选择"
                           disabled
                           clearable>
                  <el-option v-for="[key, val] in _dictTransportModeType"
                             :key="key"
                             :label="val"
                             :value="key"></el-option>
                </el-select>
              </el-col>
            </template>
          </el-table-column>
          <el-table-column label="费率">
            <template slot-scope="scope">
              <el-input v-model="scope.row.rate"
                        disabled></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <br />
      <el-row>
        <el-col :span="3">
          <h3>优惠规则设置 </h3>
        </el-col>
      </el-row>
      <hr />
      <br />
      <el-row class="box"
              v-for="(item,index) in form.preferentials"
              :key="item.id">
        <el-row class="rule">
          <el-col>计费规则：{{item.feeRuleName}}</el-col>
        </el-row>
        <el-row class="block">
          <el-col>
            有效期:{{item.startDate}}~{{item.endDate}}
          </el-col>
        </el-row>
        <el-table :data="item.items"
                  border>
          <el-table-column label="序号"
                           width="50">
            <template slot-scope="scope">
              {{ scope.$index+1 }}
            </template>
          </el-table-column>
          <el-table-column :key="item2.id"
                           :label="item2.name"
                           v-for="(item2, index2) in item.tableList">
            <template slot-scope="scope">
              <el-row v-if="item.items[scope.$index].factors[index2].name === '货类'">
                <!-- <el-col :span="12">
                  <el-select v-model="item.items[scope.$index].factors[index2].operator"
                             placeholder="请选择"
                             clearable>
                    <el-option v-for="[key, val] in _dictRuleOperator"
                               :key="key"
                               :label="val"
                               :value="key"></el-option>
                  </el-select>
                </el-col> -->
                <el-col>
                  <el-select v-model="item.items[scope.$index].factors[index2].typeValue"
                             placeholder="请选择"
                             disabled
                             clearable>
                    <el-option v-for="item in selectThingsType"
                               :key="item.cargoTypeCode"
                               :label="item.cargoTypeName"
                               :value="item.cargoTypeCode"></el-option>
                  </el-select>
                </el-col>
              </el-row>
              <el-row v-else-if="item.items[scope.$index].factors[index2].name === '品名'">
                <!-- <el-col :span="12">
                  <el-select v-model="item.items[scope.$index].factors[index2].operator"
                             placeholder="请选择"
                             clearable>
                    <el-option v-for="[key, val] in _dictRuleOperator"
                               :key="key"
                               :label="val"
                               :value="key"></el-option>
                  </el-select>
                </el-col> -->
                <el-col>
                  <el-select v-model="item.items[scope.$index].factors[index2].typeValue"
                             placeholder="请选择"
                             disabled
                             clearable>
                    <el-option v-for="item in selectGoodsType"
                               :key="item.cargoCode"
                               :label="item.cargoName"
                               :value="item.cargoCode"></el-option>
                  </el-select>
                </el-col>
              </el-row>
              <el-row v-else>
                <el-input v-model="item.items[scope.$index].factors[index2].typeValue"
                          disabled></el-input>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column label="优惠金额">
            <template slot-scope="scope">
              <el-input v-model="scope.row.rate"
                        disabled></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-col :span="3">
          <h3>入库物资设定 </h3>
        </el-col>
      </el-row>
      <hr />
      <br />
      <div>
        <el-row>
          <el-col :span="7">
            <el-form-item label="品名">
              <auto-cargo searchName="cargoName"
                          v-model="filterText"
                          @select="v=>condition['search_LIKE_cargoName']=v"
                          type="name"></auto-cargo>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-button type="primary"
                       icon="el-icon-search">查 询</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <!-- 树形菜单 -->
            <el-tree :data="treeData"
                     show-checkbox
                     node-key="id"
                     ref="tree"
                     highlight-current
                     :default-expanded-keys="selectData"
                     :props="defaultProps"
                     :filter-node-method="filterNode">
            </el-tree>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div slot="footer"
          class="dialog-footer">
      <el-button @click="auditPass" type="primary">审核通过</el-button>
      <el-button @click="auditFailed" type="danger">审核不通过</el-button>
      <el-button @click="dialogForm = false">返 回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRuleIdRate } from '@/api/balance/balance' //费率
import { auditPass, auditFailed } from '@/api/commerce/contract' //审核
import { getWarehouseList } from '@/api/adminWarehouse/adminWarehouse' //所属仓库来源于管辖仓库
import { getCargoTypeList } from '@/api/baseResource/baseResource' //货物种类的下拉菜单
import { getCargoNameList } from '@/api/baseResource/baseResource' //品名的下拉菜单
import { getAccountList } from '@/api/commerce/account'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoCargo from '@/components/autocomplete/autoCargo'
import attachments from '@/components/file-upload'
import download from '@/components/download'

export default {
  name: 'contractForm',
  components: {
    attachments,
    download,
    autoSearchOrg,
    autoCargo
  },
  data() {
    return {
      form: '',
      dialogForm: false,
      checked: '',
      checked2: '',
      checked3: '',
      value: '',
      filterText: '',
      selectGoodsType: [], //品名的下拉菜单列表
      treeData: [], //树形结构
      typeData: null,
      condition: {}, // 查询条件
      multipleSelection: [], // 选择的行
      checkList: ['非定制费率'],
      // 树形数据
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      accountList: [], //账户名称下拉菜单
      warehouseList: [],
      selectThingsType: [], //货类
      selectData: [],
      auditList: '',
      id: '',
      contractStatus: ''
    }
  },
  mounted() {
    getWarehouseList().then(res => {
      this.warehouseList = res.data.aaData
    })
    getAccountList().then(res => {
      let data = res.data.aaData
      for (let i = 0; i < data.length; i++) {
        this.accountList.push({ name: data[i].name })
      }
    })
    //货类
    getCargoTypeList().then(res => {
      this.selectThingsType = res.data.aaData
    })
    // 加载树形菜单
    getCargoNameList().then(res => {
      this.selectGoodsType = res.data.aaData
      let data = res.data.aaData
      this.typeData = data
      for (let i = 0; i < data.length; i++) {
        let noData = true
        for (let j = 0; j < this.treeData.length; j++) {
          if (data[i].cargoTypeName === this.treeData[j].label) {
            this.treeData[j].children.push({
              id: data[i].id,
              label: data[i].cargoName,
              code: data[i].cargoCode
            })
            noData = false
            break
          }
        }
        if (noData) {
          this.treeData.push({
            id: data[i].cargoTypeId,
            label: data[i].cargoTypeName,
            code: data[i].cargoTypeCode,
            children: [
              {
                id: data[i].id,
                label: data[i].cargoName,
                code: data[i].cargoCode
              }
            ]
          })
        }
      }
    })
  },
  methods: {
    getData() {
      //传递审核id和状态
      this.id = this.form.id
      this.contractStatus = this.form.contractStatus
      //加载计费规则表格
      let row = this.form.contractRules
      this.form.contractRules = []
      for (let i = 0; i < row.length; i++) {
        //回填表格
        getRuleIdRate(row[i].id).then(res => {
          let datas = res.data.aaData
          // 生成tableList
          let tmpList = []
          if (datas.length > 0) {
            for (let k = 0; k < datas[0].factor.length; k++) {
              let tmpll = Object.assign({}, datas[0].factor[k])
              tmpList.push(tmpll)
            }
          }
          this.form.contractRules.push({
            id: row[i].id,
            feeRuleName: row[i].feeRuleName,
            rates: datas,
            tableList: tmpList,
            startDate: row[i].startDate,
            endDate: row[i].endDate
          })
        })
      }
      //加载优惠规则表格
      for (let i = 0; i < this.form.preferentials.length; i++) {
        for (let j = 0; j < this.form.preferentials[i].items.length; j++) {
          this.form.preferentials[i].tableList = this.form.preferentials[
            i
          ].items[j].factors
        }
      }
      //父组件传值,加载选中的树形菜单
      this.$nextTick(function() {
        if (this.form.contractCargos) {
          let selectData = []
          this.treeData.forEach(row => {
            for (let i = 0; i < this.form.contractCargos.length; i++) {
              for (let j = 0; j < row.children.length; j++) {
                if (
                  this.form.contractCargos[i].cargoCode === row.children[j].code
                ) {
                  selectData.push({
                    id: row.children[j].id,
                    label: row.children[j].label
                  })
                }
              }
            }
          })
          this.$refs.tree.setCheckedNodes(selectData)
          this.selectData = selectData
        } else {
          this.$refs.tree.getCheckedNodes([])
        }
      })
    },
    // 树形菜单
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    //审核通过
    auditPass() {
      this.contractStatus = 'C'
      auditPass(this.id).then(res => {
        this.$message({ type: 'success', message: '审核通过' })
        this.$emit('load')
        this.dialogForm = false
      })
    },
    //审核不通过
    auditFailed() {
      this.$prompt('请输入不通过的原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(({ value }) => {
          this.failedReason = value
          this.contractStatus = 'R'
          auditFailed(this.id, { failedReason: this.failedReason }).then(
            res => {
              this.form.failedReason = value
              this.$message({ type: 'info', message: '审核不通过' })
              this.$emit('load')
              this.$emit('aload', true)
              this.dialogForm = false
              console.log('审核不过')
              console.log(this.form)
            }
          )
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.box {
  border: 2px solid #eee;
  margin: 20px 0;
}
.box .title,
.box .rule {
  background: #eee;
  height: 50px;
  line-height: 50px;
  padding-left: 10px;
}
.block {
  margin: 20px 0;
}
.el-height {
  margin: 10px 0;
}
</style>
